﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Corrmirt
{
    class pase
    {
        // 效验文件资源是否存在
        public void verification()
        {
            if (!System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory() + @".\user.json"))
            {
                System.IO.File.WriteAllText(System.IO.Directory.GetCurrentDirectory() + "\\user.json", "[]", Encoding.UTF8);
                System.Windows.MessageBox.Show("缺失一份文件，我们以为你创建!\r\nA file is missing, we thought you created it!");
            }
            if (!System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory() + @".\ip.ini"))
            {
                System.IO.File.WriteAllText(System.IO.Directory.GetCurrentDirectory() + "\\ip.ini", "ip=0.0.0.0", Encoding.UTF8);
                System.Windows.MessageBox.Show("ip.ini文件丢失，以为你新建，IP需要自己写!\r\nThe ip.ini file is missing, I thought you created a new one, and the IP needs to be written by yourself!");
            }
            if (!System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory() + @".\frpc.exe"))
            {
                System.IO.File.WriteAllText(System.IO.Directory.GetCurrentDirectory() + "\\user.json", "[]", Encoding.UTF8);
                System.Windows.MessageBox.Show("frpc.exe程序丢失需要手动下载，地址：https://github.com/fatedier/frp/tags \r\nThe frpc.exe program is missing and needs to be downloaded manually, address:https://github.com/fatedier/frp/tags");
                System.Environment.Exit(0);
            }
        }
        public JArray pase_json()
        {
            string text = System.IO.File.ReadAllText(System.IO.Directory.GetCurrentDirectory() + @".\user.json");
            Console.WriteLine("text");
            JArray jsons = JArray.Parse(text);
            if(jsons.Count == 0)
            {
                return null;
            }
            return jsons;
            //System.Diagnostics.Debug.WriteLine();
        }

        public string generate_json(string Name,string type,string IP,string port,string reme_port)
        {
            return "{" + string.Format("\"Name\":\"{0}\",\"local_ip\":\"{1}\",\"local_port\":\"{2}\",\"type\":\"{3}\",\"Outside_Port\":\"{4}\"",Name,IP,port,type, reme_port) + "}";
        }
        public string generate_wtire_json(string json) {
            JArray jsons = pase_json();
            string wirte_json = "";
            wirte_json += "[";
            //解析json
            if (jsons != null)
            {
                for (int i = 0; i < jsons.Count; i++)
                {
                    if(jsons.Count != i)
                    {
                        wirte_json += jsons[i] + ",";
                    }
                }
                wirte_json += json;
            }
            else
            {
                wirte_json += json;
            }
            wirte_json += "]";
            System.Diagnostics.Debug.WriteLine(wirte_json);
            System.IO.File.WriteAllText(System.IO.Directory.GetCurrentDirectory() + "\\user.json", wirte_json, Encoding.UTF8);
            return "OK";
        }
        public string move_json(string tag)
        {
            JArray jsons = pase_json();
            string wirte_json = "";
            wirte_json += "[";
            //解析json
            if (jsons.Count == 1){  }
            else
            {
                for (int i = 0; i < jsons.Count; i++)
                {
                    if (i.ToString() == tag) { }
                    else
                    {
                        if (jsons.Count - 1 > i)
                        {
                            wirte_json += jsons[i] + ",";
                        }
                        else
                        {
                            wirte_json += jsons[i];
                        }
                    }
                }
            }
            wirte_json += "]";
            System.IO.File.WriteAllText(System.IO.Directory.GetCurrentDirectory() + "\\user.json", wirte_json, Encoding.UTF8);
            //System.Diagnostics.Debug.WriteLine("======= 生成的Json =======\r\n" + wirte_json);
            return "OK";
        }
        // frpc run common on
        public void Run_frp()
        {
            Process frpc = new Process();
            frpc.StartInfo.FileName = System.IO.Directory.GetCurrentDirectory() + "./frpc.exe";
            frpc.StartInfo.Arguments = "-c frpc.ini";
            frpc.StartInfo.CreateNoWindow = true;
            frp_ini();
            frpc.Start();
            //frpc.Exited();
        }
        // Reding ini IP
        public string Read_ini(string loct_path)
        {
            string text = System.IO.File.ReadAllText(System.IO.Directory.GetCurrentDirectory() + loct_path);
            string[] spig = text.Split('=');
            return spig[1];
        }
        public void frp_ini()
        {
            string ini = string.Empty;
            JArray jsons = pase_json();
            if (jsons != null)
            {
                ini = "[common]\r\n";
                ini += "server_addr = " + Read_ini("\\ip.ini");
                ini += "\r\nserver_port = 7000\r\n\r\n";
                for (int i = 0; i < jsons.Count; i++)
                {
                    ini += "[" + jsons[i]["Name"] + "]";
                    ini += "\r\ntype = " + jsons[i]["type"];
                    ini += "\r\nlocal_ip = " + jsons[i]["local_ip"];
                    ini += "\r\nlocal_port = " + jsons[i]["local_port"];
                    ini += "\r\nremote_port = " + jsons[i]["Outside_Port"];
                    ini += "\r\nuse_compression = true\r\n";
                }
            }
            System.IO.File.WriteAllText(System.IO.Directory.GetCurrentDirectory() + "\\frpc.ini", ini, Encoding.UTF8);
            System.Diagnostics.Debug.WriteLine(ini);
        }
    }
}
